<template>
    <div class="app-container" style="height: 700px;overflow-y: auto">
        <div style="position: absolute; right: 30px;">
            <div>
                <el-button type="primary" @click="recordPrint">
                    <template #icon>
                        <i-ep-printer/>
                    </template>
                    病历打印
                </el-button>
                <el-button type="primary" @click="getList">
                    <template #icon>
                        <i-ep-search/>
                    </template>
                    刷新
                </el-button>
            </div>
        </div>
        <el-col :span="18" style="padding-bottom: 5px">
            <el-form ref="formRef" :model="form" label-width="100px">
                <el-row>
                    <el-col :span="4">
                        <el-form-item label="就诊时间：">
                            <span class="mt-2">{{ clinicInfo.visitDate }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="就诊科室：">
                            <span class="mt-2">{{ clinicInfo.deptName }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="民族：">
                            <span class="mt-2"> {{
                                    dictStore.getMateDictInfo(dictDataMap['nation_dict'], clinicInfo.nation)
                                        ?
                                        dictStore.getMateDictInfo(dictDataMap['nation_dict'],
                                            clinicInfo.nation).dictLabel
                                        : ''
                                }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="婚姻：">
                            <span class="mt-2"> {{
                                    dictStore.getMateDictInfo(dictDataMap['marriage_dict'], clinicInfo.marriage)
                                        ?
                                        dictStore.getMateDictInfo(dictDataMap['marriage_dict'],
                                            clinicInfo.marriage).dictLabel
                                        : ''
                                }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="初/复诊：">
                                <span class="mt-2"> {{
                                        clinicInfo.firstVisitIndicator === 'Y' ? '初诊' : '复诊'
                                    }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="16">
                        <el-form-item label="发病日期：" prop="fabingriqi">
                            {{ form.fabingriqi ? form.fabingriqi : '无' }}
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="急诊：">
                            <span class="mt-2"> {{
                                    dictStore.getMateDictInfo(dictDataMap['sys_yes_no'], clinicInfo.isEmergency)
                                        ?
                                        dictStore.getMateDictInfo(dictDataMap['sys_yes_no'],
                                            clinicInfo.isEmergency).dictLabel
                                        : ''
                                }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-form-item label="主诉" prop="zhusu">
                    <el-input v-model="form.zhusu" :disabled="true" :rows="4" placeholder="请输入内容" type="textarea"/>
                </el-form-item>
                <el-form-item label="现病史" prop="xianbingshi">
                    <el-input v-model="form.xianbingshi" :disabled="true" :rows="3" placeholder="请输入内容"
                              type="textarea"/>
                </el-form-item>
                <el-form-item label="流行病学史" prop="liuxingbingshi">
                    <el-input v-model="form.liuxingbingshi" :disabled="true" :rows="3" placeholder="请输入内容"
                              type="textarea"/>
                </el-form-item>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="既往史" prop="jiwangshi">
                            <el-input v-model="form.jiwangshi" :disabled="true" :rows="3" placeholder="请输入内容"
                                      type="textarea"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="个人史" prop="gerenshi">
                            <el-input v-model="form.gerenshi" :disabled="true" :rows="3" placeholder="请输入内容"
                                      type="textarea"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="婚育史" prop="hunyushi">
                            <el-input v-model="form.hunyushi" :disabled="true" :rows="3" placeholder="请输入内容"
                                      type="textarea"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="家族史" prop="jiazushi">
                            <el-input v-model="form.jiazushi" :disabled="true" :rows="3" placeholder="请输入内容"
                                      type="textarea"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="月经史" prop="yuejingshi">
                            <el-input v-model="form.yuejingshi" :disabled="true" :rows="3" placeholder="请输入内容"
                                      type="textarea"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="发病后一般情况" prop="fabinghouqingkuang">
                            <el-input v-model="form.fabinghouqingkuang" :disabled="true" :rows="3"
                                      placeholder="请输入内容"
                                      type="textarea"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-form-item label="体格检查I">
                        <el-col :span="4">
                            <el-form-item class="tigejiancha" label="T ：" label-width="50px" prop="tiwen">
                                <el-input v-model="form.tiwen" :disabled="true" :rows="3" class="tigejiancha-input"
                                          clearable
                                          placeholder="体温"
                                          style="width:120px">
                                    <template #append>℃</template>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item class="tigejiancha" label="P ：" label-width="100px" prop="maibo">
                                <el-input v-model="form.maibo" :disabled="true" :rows="3" class="tigejiancha-input"
                                          clearable
                                          placeholder="脉搏"
                                          style="width:120px">
                                    <template #append>次/分</template>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item class="tigejiancha" label="R ：" label-width="120px" prop="huxi">
                                <el-input v-model="form.huxi" :disabled="true" :rows="3" class="tigejiancha-input"
                                          clearable
                                          placeholder="呼吸"
                                          style="width:120px">
                                    <template #append>次/分</template>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item class="tigejiancha" label="BP ：" label-width="150px">
                                <el-input v-model="form.systolicPressure" :disabled="true" :rows="4"
                                          class="tigejiancha-input" clearable
                                          placeholder="收缩压" style="width:50px"/>
                                <span style="margin:0 11px 0 10px">/</span>
                                <el-input v-model="form.diastolicPressure" :disabled="true" :rows="4"
                                          class="tigejiancha-input" clearable
                                          placeholder="舒张压" style="width:50px">
                                    <template #append>mmHg</template>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item class="tigejiancha" label="FBG ：" label-width="90px"  >
                                <el-input v-model="form.fbg" :disabled="true" :rows="4" class="tigejiancha-input"
                                          clearable
                                          placeholder="空腹血糖"
                                          style="width:120px">
                                    <template #append>mmol/L</template>
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-form-item>
                </el-row>
                <el-form-item label="体格检查II" prop="tigejiancha">
                    <el-input v-model="form.tigejiancha" :disabled="true" :rows="4" placeholder="请输入内容"
                              type="textarea"/>
                </el-form-item>
                <el-form-item label="诊断">
                    <el-table :border="true" :data="hisDiagnosisList" :header-cell-style="$headerCellStyle" :stripe="true"
                              height="180" highlight-current-row>
                        <el-table-column :align="'center'" label="西/中医诊断" prop="type" width="100">
                            <template #default="scope">
                                <dict-data-tag :options="dictDataMap['chinese_western_type']" :value="scope.row.type"/>
                            </template>
                        </el-table-column>
                        <el-table-column :align="'center'" label="诊断类型" prop="diagnosisType" width="120">
                            <template #default="scope">
                                <dict-data-tag :options="dictDataMap['diagnosis_type']"
                                               :value="scope.row.diagnosisType"/>
                            </template>
                        </el-table-column>
                        <el-table-column :align="'center'" label="名称" prop="icdName" width="200">
                            <template #default="scope">
                                <span> {{ scope.row.icdName }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column :align="'center'" label="编码" prop="icdCode" width="200">
                            <template #default="scope">
                                <span> {{ scope.row.icdCode }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column :align="'center'" label="诊断医生" prop="diagnosisDoctorName" width="99">
                            <template #default="scope">
                                <span> {{ scope.row.diagnosisDoctorName }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column :align="'center'" label="排序" prop="sort" width="100">
                            <template #default="scope">
                                <span> {{ scope.row.sort }}</span>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-form-item>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="过敏史" prop="allergyHistory">
                            <el-input v-model="form.allergyHistory" :disabled="true" :rows="3" placeholder="请输入内容"
                                      type="textarea"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="药物过敏史" prop="drugAllergyHistory">
                            <el-input v-model="form.drugAllergyHistory" :disabled="true" :rows="3"
                                      placeholder="请输入内容"
                                      type="textarea"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="辅助检查" prop="fuzhujiancha">
                    <el-input v-model="form.fuzhujiancha" :disabled="true" :rows="3" placeholder="请输入内容"
                              type="textarea"/>
                </el-form-item>
                <el-form-item label="小结分析" prop="xiaojiefenxi">
                    <el-input v-model="form.xiaojiefenxi" :disabled="true" :rows="3" placeholder="请输入内容"
                              type="textarea"/>
                </el-form-item>
                <!-- <el-form-item label="病史采集日期" prop="bingshicaijiriqi">
                    <el-date-picker clearable v-model="form.bingshicaijiriqi" type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择病史采集日期">
                    </el-date-picker>
                </el-form-item> -->
                <el-form-item label="处理" prop="dispose">
                    <el-input v-model="form.dispose" :disabled="true" :rows="3" placeholder="请输入内容"
                              type="textarea"/>
                </el-form-item>
                <el-form-item label="建议" prop="suggestion">
                    <el-input v-model="form.suggestion" :disabled="true" :rows="3" placeholder="请输入内容"
                              type="textarea"/>
                </el-form-item>
                <!-- <el-form-item label="备注信息" prop="remarks">
                    <el-input v-model="form.remarks" placeholder="请输入备注信息" />
                </el-form-item> -->
            </el-form>
        </el-col>
<!--        <pdf-preview ref="printPreviewRef" :title="'门诊病历'"/>-->
    </div>
</template>
<script lang="ts">
export default {
    name: "ClinicRecord"
}
</script>

<script lang="ts" setup>
import {
    listClinicRecord
} from "@/api/diagnosis/clinicRecord";
import {useDictStoreHook} from "@/store/modules/dict"
import {useUserStoreHook} from "@/store/modules/user"
import pdfPreview from "@/components/PrintPreview/PdfPreview.vue"
import {getClinicMaster} from "@/api/patient/clinicMaster.ts";
import {listHisDiagnosis} from "@/api/diagnosis/hisDiagnosis.ts";

const dictStore = useDictStoreHook()
let dictDataMap = ref(dictStore.isTypeGetData('sys_yes_no',
    'sys_user_sex', 'marriage_dict', 'nation_dict', 'nationality_dict', 'diagnosis_type', 'chinese_western_type'))
const useUserStore = useUserStoreHook()
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo
const {proxy} = getCurrentInstance() as any;
const clinicInfo = ref<any>({})
const formRef = ref();
const objectUrl = ref("")
const printPreviewRef = ref();
const hisDiagnosisList = ref<any>([])
const form = ref<any>({
    clinicId: "",
    patientId: "",
    fabingriqi: "",
    zhusu: "",
    liuxingbingshi: "",
    xianbingshi: "",
    jiwangshi: "",
    gerenshi: "",
    hunyushi: "",
    jiazushi: "",
    yuejingshi: "",
    allergyHistory: "",
    drugAllergyHistory: "",
    fabinghouqingkuang: "",
    tiwen: "",
    maibo: "",
    huxi: "",
    xueya: "",
    systolicPressure: "",
    diastolicPressure: "",
    bingshichenshuzhe: "",
    tigejiancha: "",
    fuzhujiancha: "",
    xiaojiefenxi: "",
    bingshicaijiriqi: "",
    dispose: "",
    suggestion: "",
    orgId: userInfo.orgId,
    remarks: "",
    symptomaticType: "",
    printDate: "",
    printUser: "",
    yiqing: "",
    fbg: "",
    healthPreach: "",
    mmmtbzmldm: "",
    mmmtbzmc: "",
    abzffbzdm: "",
    abzffbzmc: ""
})

// 检查
function getClinicRecordList(clinicId: string) {
    form.value.clinicId = clinicId
    getList()
}

// 查询诊断
function listHisDiagnosisFun(clinicId) {
    listHisDiagnosis({clinicId: clinicId, inoroutFlag: '4'}).then((res: any) => {
        hisDiagnosisList.value = res.data
    })
}

function getList() {
    const obj = {
        clinicId: form.value.clinicId
    }
    reset();
    getClinicMaster(obj.clinicId).then((response: any) => {
        clinicInfo.value = response.data;
        form.value.clinicId = clinicInfo.value.id
    })
    listClinicRecord(obj).then((response: any) => {
        if (response.data.length) {
            form.value = response.data[0]
            form.value.clinicId = response.data[0]
        }
    })
    listHisDiagnosisFun(obj.clinicId)
}


/**
 * 病历打印
 */
function recordPrint() {
    let url = "/business/clinicRecord/recordPrint";
    const formData = new FormData()
    formData.append("id", form.value.id)
    formData.append("orgId", form.value.orgId)
    // pdf打印 、打印预览
    proxy.$printPdfPublic(url, formData).then(res => {
        if (res) {
            objectUrl.value = URL.createObjectURL(res)
            // nextTick(() => {
            // printPreviewRef.value.init(objectUrl.value)
            // })
        } else {
            console.error('打印失败')
        }
    })
}


// 表单重置
function reset() {
    form.value = {
        id: "",
        clinicId: "",
        patientId: "",
        fabingriqi: "",
        zhusu: "",
        liuxingbingshi: "",
        xianbingshi: "",
        jiwangshi: "",
        gerenshi: "",
        hunyushi: "",
        jiazushi: "",
        yuejingshi: "",
        allergyHistory: "",
        drugAllergyHistory: "",
        fabinghouqingkuang: "",
        tiwen: "",
        maibo: "",
        huxi: "",
        xueya: "",
        systolicPressure: "",
        diastolicPressure: "",
        bingshichenshuzhe: "",
        tigejiancha: "",
        fuzhujiancha: "",
        xiaojiefenxi: "",
        bingshicaijiriqi: "",
        dispose: "",
        suggestion: "",
        orgId: "",
        delFlag: "",
        createBy: "",
        createTime: "",
        updateBy: "",
        updateTime: "",
        remarks: "",
        symptomaticType: "",
        printDate: "",
        printUser: "",
        yiqing: "",
        fbg: "",
        healthPreach: "",
        mmmtbzmldm: "",
        mmmtbzmc: "",
        abzffbzdm: "",
        abzffbzmc: ""
    };
    nextTick(() => {
        formRef.value.resetFields()
    })
}

defineExpose({
    getClinicRecordList
})
</script>

<style scoped>
.mt-2 {
    margin-top: -2px
}

.tigejiancha {
    margin-left: -14px;
}

:deep(.el-input-group__append) {
    width: 40px;
    padding: 0 5px;
}

:deep(.tigejiancha-input) {
    margin-left: -10px
}

:deep(.tigejiancha-input .el-input__wrapper .el-input__inner) {
    width: 45px
}

:deep(.el-form-item__error) {
    width: 140px;
}
</style>
